﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Customs.Components.KDT.SXXK.Info;
using Microsoft.ApplicationBlocks.Data;

namespace Customs.Components.KDT.SXXK
{
    public partial class ThongTinDinhMuc : ThongTinDinhMucInFo
    {
        public void Load(ThongTinDinhMucInFo info)
        {
            this._So_TNDKDT = info.So_TNDKDT;
            this._MA_DV = info.MA_DV;
            this._Ma_DVKB = info.Ma_DVKB;;
            this._MA_HQTN = info.MA_HQTN;
            this._Nam_TN = info.Nam_TN;
            this._Ngay_TN = info.Ngay_TN;
            this._TrangThaiXL = info.TrangThaiXL;
            this.dmSanPham = info.DinhMucSanPham;
        }

        public void Load()
        {
            SqlDataReader reader = this.Select();
            if (reader.Read())
            {
                if (!reader.IsDBNull(reader.GetOrdinal("SoTiepNhan"))) this.So_TNDKDT = reader.GetInt64(reader.GetOrdinal("SoTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHaiQuan"))) this.MA_HQTN = reader.GetString(reader.GetOrdinal("MaHaiQuan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NamTiepNhan"))) this.Nam_TN = reader.GetInt16(reader.GetOrdinal("NamTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgayTiepNhan"))) this.Ngay_TN = reader.GetDateTime(reader.GetOrdinal("NgayTiepNhan"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDoanhNghiep"))) this.MA_DV = reader.GetString(reader.GetOrdinal("MaDoanhNghiep"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaDaiLy"))) this.Ma_DVKB = reader.GetString(reader.GetOrdinal("MaDaiLy"));
                if (!reader.IsDBNull(reader.GetOrdinal("TrangThaiXuLy"))) this.TrangThaiXL = reader.GetInt32(reader.GetOrdinal("TrangThaiXuLy"));             
                reader.Close();               
            }           
        }

        public SqlDataReader Select()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString;
            StringBuilder query = new StringBuilder();
            query.Append("SELECT ");
            query.Append("So_TNDKDT AS [SoTiepNhan], ");
            query.Append("Ma_HQTN AS [MaHaiQuan], ");
            query.Append("Nam_TN AS [NamTiepNhan], ");
            query.Append("Ngay_TN AS [NgayTiepNhan], ");
            query.Append("Ma_DV AS [MaDoanhNghiep], ");          
            query.Append("Ma_DVKB AS [MaDaiLy], ");
            query.Append("TrangThaiXL AS [TrangThaiXuLy], ");
            query.Append("AppSource ");
            query.Append("FROM dbo.[DDINHMUCDK] ");
            query.Append("WHERE ");
            query.Append("So_TNDKDT = @SoTiepNhan AND Ma_HQTN = @MaHaiQuan AND Nam_TN = @NamTiepNhan");

            SqlParameter[] parameters = new SqlParameter[3];
            parameters[0] = new SqlParameter("@SoTiepNhan", SqlDbType.BigInt);
            parameters[0].Value = this.So_TNDKDT;

            parameters[1] = new SqlParameter("@MaHaiQuan", SqlDbType.Char, 6);
            parameters[1].Value = this.MA_HQTN;

            parameters[2] = new SqlParameter("@NamTiepNhan", SqlDbType.SmallInt);
            parameters[2].Value = this.Nam_TN;

            return SqlHelper.ExecuteReader(connectionString, CommandType.Text, query.ToString(), parameters);
        }
        public void InsertTransaction(SqlTransaction transaction)
        {
            StringBuilder query = new StringBuilder();
            query.Append("INSERT INTO DDINHMUCDK (");
            query.Append("So_TNDKDT, ");
            query.Append("Ma_HQTN, ");
            query.Append("Nam_TN, ");
            query.Append("Ngay_TN, ");
            query.Append("MA_DV, ");
            query.Append("Ma_DVKB, ");
            query.Append("TrangThaiXL, ");
            query.Append("AppSource ) ");

            query.Append("VALUES ( ");

            query.Append("@So_TNDKDT, ");
            query.Append("@Ma_HQTN, ");
            query.Append("@Nam_TN, ");
            query.Append("@Ngay_TN, ");
            query.Append("@MA_DV, ");
            query.Append("@Ma_DVKB, ");
            query.Append("@TrangThaiXL, ");
            query.Append("@AppSource )");
            SqlParameter[] parameters = new SqlParameter[8];

            parameters[0] = new SqlParameter("@So_TNDKDT", SqlDbType.BigInt);
            parameters[0].Value = this.So_TNDKDT;

            parameters[1] = new SqlParameter("@MA_HQTN", SqlDbType.Char, 6);
            parameters[1].Value = this.MA_HQTN;

            parameters[2] = new SqlParameter("@Nam_TN", SqlDbType.SmallInt);
            parameters[2].Value = this.Nam_TN;          

            parameters[3] = new SqlParameter("@Ngay_TN", SqlDbType.DateTime);
            parameters[3].Value = this.Ngay_TN;
           
            parameters[4] = new SqlParameter("@MA_DV", SqlDbType.VarChar, 14);
            parameters[4].Value = this.MA_DV;

            parameters[5] = new SqlParameter("@Ma_DVKB", SqlDbType.VarChar, 14);
            parameters[5].Value = this.Ma_DVKB;

            parameters[6] = new SqlParameter("@TrangThaiXL", SqlDbType.Int);
            parameters[6].Value = this.TrangThaiXL;

            parameters[7] = new SqlParameter("@AppSource", SqlDbType.NVarChar, 50);
            parameters[7].Value = this._AppSource;

            SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query.ToString(), parameters);
        }

        public long GetSoTiepNhan(SqlTransaction transaction)
        {
            string spName = "KDT_CapSo_DM";
            SqlParameter[] parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@MaHQTN", SqlDbType.Char, 6);
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = this.MA_HQTN;

            parameters[1] = new SqlParameter("@NamTN", SqlDbType.SmallInt);
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = this.Nam_TN;

            return Convert.ToInt64(SqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName, parameters));
            return (long)parameters[2].Value;
        }

        //-----------------------------------------------------------------------------------------

        public long InsertFull()
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString))
            {
                conn.Open();
                SqlTransaction transactionKDT = conn.BeginTransaction();
                try
                {
                    if (this.So_TNDKDT > 0)
                        HuyThongTin(this.So_TNDKDT, this.MA_HQTN, this.MA_DV, this.Nam_TN, transactionKDT);
                    this.Ngay_TN = DateTime.Today;
                    this.Nam_TN = (short)DateTime.Today.Year;
                    this.So_TNDKDT = this.GetSoTiepNhan(transactionKDT);                  
                    this.InsertTransaction(transactionKDT);

                    // Danh sách định mức.                    
                    foreach (DinhMucDangKyInfo dmdkInfo in this.dmSanPham)
                    {
                        int i = 1;
                        DinhMucDangKy dmdk = new DinhMucDangKy();
                        dmdk.Load(dmdkInfo);
                        dmdk.SoTiepNhan = this.So_TNDKDT;
                        dmdk.NamTiepNhan = this.Nam_TN;
                        dmdk.MaHaiQuan = this.MA_HQTN;                        
                        dmdk.Insert(transactionKDT);
                        foreach (DinhMucInfo dmInfo in dmdkInfo.DMInfoCollection)
                        {
                            dmInfo.SoTiepNhan = this._So_TNDKDT;
                            dmInfo.MaHaiQuan = this._MA_HQTN;
                            dmInfo.NamTiepNhan = this._Nam_TN;
                            dmInfo.MaDoanhNghiep = this._MA_DV;
                            dmInfo.SoThuTuHang = i++;
                            DinhMuc dm = new DinhMuc();
                            dm.Load(dmInfo);
                            dm.InsertTransaction(transactionKDT);
                        }
                    }                   
                    transactionKDT.Commit();
                    return this.So_TNDKDT;
                }
                catch (Exception ex)
                {
                    transactionKDT.Rollback();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        //-----------------------------------------------------------------------------------------


        public static bool HuyThongTinKhaiBao(long soTiepNhan, string maHaiQuan, string maDoanhNghiep, int namTiepNhan)
        {
            bool ret;
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString_KDT"].ConnectionString))
            {
                conn.Open();
                SqlTransaction transaction = conn.BeginTransaction();
                try
                {
                    HuyThongTin(soTiepNhan, maHaiQuan, maDoanhNghiep, namTiepNhan, transaction);
                    ret = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
            return ret;
        }
        public static void HuyThongTin(long soTiepNhan, string maHaiQuan, string maDoanhNghiep, int namTiepNhan,SqlTransaction transaction)
        {
            ThongTinDinhMuc ttdm = new ThongTinDinhMuc();
            ttdm.So_TNDKDT = soTiepNhan;
            ttdm.MA_HQTN = maHaiQuan;
            ttdm.Nam_TN = (short)namTiepNhan;
            ttdm.Load();
            if (ttdm.TrangThaiXL == 1)
                throw new Exception("123DaDuyet321");
            // Xóa các định mức.
            DinhMuc.Delete(transaction, soTiepNhan, maHaiQuan, maDoanhNghiep, namTiepNhan);

            // Xóa thông tin khai báo.
            DinhMucDangKy.Delete(transaction, soTiepNhan, maHaiQuan, maDoanhNghiep, namTiepNhan);

            //Xóa thông định mức chung
            ttdm.DeleteTransaction(transaction);
           
        }

      
        //---------------------------------------------------------------------------------------------
       
        #region Delete methods.

        public int Delete()
        {
             this.DeleteTransaction(null);
             return 1;            
        }

        //---------------------------------------------------------------------------------------------

        public void DeleteTransaction(SqlTransaction transaction)
        {
            string query = string.Format("DELETE FROM DDINHMUCDK WHERE SO_TNDKDT = {0} AND MA_HQTN = '{1}' AND MA_DV = '{2}' AND NAM_TN = {3} ", this.So_TNDKDT,MA_HQTN.PadRight(6), MA_DV, Nam_TN);
            SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, query);
      
        }

        //---------------------------------------------------------------------------------------------
      

        #endregion
    }
}